//
//  CLSearchDataModel.m
//  chanlin
//
//  Created by shuhui on 15-6-3.
//  Copyright (c) 2015年 chanlin. All rights reserved.
//

#import "CLSearchDataModel.h"
#import "IFCoreDataManager.h"

#import "SearchNewsEntity.h"

@interface CLSearchDataModel ()
@property (nonatomic,retain) NSMutableSet *documentIdSet;

@end
@implementation CLSearchDataModel
SGR_DEF_SINGLETION(CLSearchDataModel)

-(instancetype)init{
    if(self = [super init]){
//        _documentIdSet = [NSMutableSet new];
//        _dateFormatter=[[NSDateFormatter alloc] init];
//        [_dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
        
    }
    return self;
}

//[_model getHotKeyWordsWithUICallBack:^(IfengData_Response *response){
//- (void)getHotKeyWordsWithUICallBack:(void (^)(IfengData_Response *))uiblock

//[[CLSearchDataModel sharedInstance] getHotKeyWordsWithUICallBack:^(BOOL success, id responseObj, NSString *message){

- (void)getHotKeyWordsWithUICallBack:(void (^)(BOOL success,id responseObj,NSError *message,NSMutableDictionary *param))block{
    
    __weak typeof(self) me=self;
    [self getWithUrl:@"http://appv2.chanlin.org/e/app/hotkey.php?os=ios" param:nil dataBlock:^(BOOL success, id responseObj, NSError *message, NSMutableDictionary *param) {
        
        if(responseObj &&
           [responseObj isKindOfClass:[NSDictionary class]]&&
           [@"1" isEqualToString:[responseObj sgrFGetStringForKey:@"status"]]&&
           [@"success" isEqualToString:[responseObj sgrFGetStringForKey:@"msg"]]){
            NSArray *dataArray = [responseObj sgrGetArrayForKey:@"data"];
            
            if (dataArray  && [dataArray count]>0) {
                if ([me.dataArray count]>0) {
                    [me.dataArray addObjectsFromArray:dataArray];
                }
                else {
                    me.dataArray = [NSMutableArray arrayWithArray:dataArray];
                }
            }
            
            
        }

    } uiBlock:block];

}

-(void)getSearchListWithKeyWord:(NSString *)keyWord pageNum:(int)pageNum uiCallBack:(void (^)(BOOL success,id responseObj,NSError *message,NSMutableDictionary *param))block {
    
//    http://appv2.chanlin.org/e/app/search.php?keyboard=禅林&os=ios
    
    
    NSDictionary *paramDic = @{@"keyboard":(_isStrNotNull(keyWord)?keyWord:@""),
                               @"page":[NSString stringWithFormat:@"%d",pageNum]
                               };
    
    
    __weak typeof(self) me=self;
    [self getWithUrl:@"http://appv2.chanlin.org/e/app/search.php?os=ios" param:paramDic dataBlock:^(BOOL success, id responseObj, NSError *message, NSMutableDictionary *param) {
        
        if(responseObj &&
           [responseObj isKindOfClass:[NSDictionary class]]&&
           [@"1" isEqualToString:[responseObj sgrFGetStringForKey:@"status"]]&&
           [@"success" isEqualToString:[responseObj sgrFGetStringForKey:@"msg"]]){
            NSArray *listMutArray = [responseObj sgrGetArrayForKey:@"data"];
            if (listMutArray  && [listMutArray count]>0) {
                if ([me.listMutArray count]>0) {
                    [me.listMutArray addObjectsFromArray:listMutArray];
                }
                else {
                    me.listMutArray = [NSMutableArray arrayWithArray:listMutArray];
                }
            }
            
            me.entityArray = [NSMutableArray new];
            for (int i=0 ; i<[listMutArray count]; i++) {
                NSDictionary *dic = [listMutArray sgrGetDictionaryForIndex:i];
                if (!dic) continue;
                
                NSString *document = [dic sgrGetStringForKey:@"id"];
                if (_isStrNotNull(document) &&![me.documentIdSet containsObject:document])
                    [me.documentIdSet sgrAddObject:document];
                else continue;
                SearchNewsEntity *entity = [SearchNewsEntity new];
                entity.title = [dic sgrGetStringForKey:@"title"];
                entity.thumbnail = [dic sgrGetStringForKey:@"titlepic"];
                entity.newstime = [dic sgrGetStringForKey:@"newstime"];
                entity.documentId = [dic sgrGetStringForKey:@"id"];
                entity.commentCount = [dic sgrGetNumberForKey:@"comment"];
                entity.linkType = [dic sgrGetStringForKey:@"type"];
                entity.showType = [dic sgrGetStringForKey:@"showtype"];
                [me.entityArray sgrAddObject:entity];
                
            }
        }
        
    } uiBlock:block];
    
}
@end
